package com.taptech.doufu.ui.fragment;

import android.app.Fragment;
import android.os.Bundle;
import androidx.annotation.Nullable;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;

import com.taptech.doufu.R;
import com.taptech.doufu.ui.dialog.NovelCreateSentTimeDialog;
import com.taptech.doufu.app.WeMediaApplication;
import com.taptech.doufu.ui.view.wheel.adapter.NumericWheelAdapter;
import com.taptech.doufu.ui.view.wheel.widget.OnWheelScrollListener;
import com.taptech.doufu.ui.view.wheel.widget.WheelView;

import java.util.Calendar;

/**
 * Created by ${myKelly} on 2016/12/1.
 */

public class SelectDateFragmet extends Fragment {
    private WheelView wheelViewYear ,wheelViewMonth,wheelViewDay;
    private NovelCreateSentTimeDialog.ChangeDate changeDateListener;
    private int curYear ;
    private int curMonth ;
    private int curDate ;
    private int curHour ;
    private int curSelectYear ;
    private int curSelectMonth ;
    private int curSelectDate ;

    private int initYear;
    private int initMonth;
    private int initDate;

    // TODO 设置透明度
    public static int[] SHADOWS_COLORS = new int[] { 0xef212222,
            0xcf212222, 0x3f212222 };
    @Override
    public void onActivityCreated(@Nullable Bundle savedInstanceState) {
        super.onActivityCreated(savedInstanceState);
        initDate();
    }

    @Nullable
    @Override
    public View onCreateView(LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {
        View rootView = inflater.inflate(R.layout.select_date_fg,null);
        wheelViewYear = (WheelView)rootView.findViewById(R.id.select_date_fg_year);
        wheelViewMonth = (WheelView)rootView.findViewById(R.id.select_date_fg_month);
        wheelViewDay = (WheelView)rootView.findViewById(R.id.select_date_fg_day);
        if(WeMediaApplication.getInstance().isDayNightMode_Night){
            wheelViewYear.setWheelBackground(R.drawable.wheel_night_bg);
            wheelViewMonth.setWheelBackground(R.drawable.wheel_night_bg);
            wheelViewDay.setWheelBackground(R.drawable.wheel_night_bg);
            wheelViewYear.setShadowColor(SHADOWS_COLORS);
            wheelViewMonth.setShadowColor(SHADOWS_COLORS);
            wheelViewDay.setShadowColor(SHADOWS_COLORS);
        }

        setWheelViewListener();
        return rootView;
    }

    private void setWheelViewListener(){
        wheelViewYear.addScrollingListener(new OnWheelScrollListener() {
            @Override
            public void onScrollingStarted(WheelView wheel) {

            }

            @Override
            public void onScrollingFinished(WheelView wheel) {
                curSelectYear = wheel.getCurrentItem() + 2000;
                Log.d("wheelViewYear","onScrollingFinished --->" +curSelectYear + "--->" + curYear);
                if(curSelectYear < curYear){
                    wheelViewYear.setCurrentItem(curYear -2000);
                    curSelectYear = curYear;
                }
                if (initYear == curSelectYear){
                    if (initMonth >= curSelectMonth){
                        curSelectMonth = initMonth;
                        wheelViewMonth.setCurrentItem(initMonth-1);
                        if (initDate >= curSelectDate) {
                            curSelectDate = initDate;
                            wheelViewDay.setCurrentItem(initDate-1);
                        }
                    }
                }
                selectDateFinish();
            }
        });
        wheelViewMonth.addScrollingListener(new OnWheelScrollListener() {
            @Override
            public void onScrollingStarted(WheelView wheel) {

            }

            @Override
            public void onScrollingFinished(WheelView wheel) {
                curSelectMonth = wheel.getCurrentItem() + 1;
                Log.d("wheelViewMonth","onScrollingFinished --->" +curSelectMonth + "--->" + curMonth);
                if(curSelectYear == curYear && curSelectMonth < curMonth ){
                    wheelViewMonth.setCurrentItem(curMonth - 1 );
                    curSelectMonth = curMonth;
                }
                if (initYear == curSelectYear && initMonth == curSelectMonth){
                    if (initDate >= curSelectDate) {
                        curSelectDate = initDate;
                        wheelViewDay.setCurrentItem(initDate-1);
                    }
                }

                selectDateFinish();
            }
        });
        wheelViewDay.addScrollingListener(new OnWheelScrollListener() {
            @Override
            public void onScrollingStarted(WheelView wheel) {

            }

            @Override
            public void onScrollingFinished(WheelView wheel) {
                curSelectDate = wheel.getCurrentItem() + 1 ;
                Log.d("wheelViewDay","onScrollingFinished --->" +curSelectDate + "--->" + curDate);
                if(curSelectYear == curYear && curSelectMonth == curMonth  && curSelectDate < curDate+1 ){
                    wheelViewDay.setCurrentItem(curDate -1 );
                    curSelectDate = curDate;
                }
                selectDateFinish();
            }
        });
    }

    private void initDate(){
        Calendar c = Calendar.getInstance();
        curYear = c.get(Calendar.YEAR);
        curMonth = c.get(Calendar.MONTH) + 1;//通过Calendar算出的月数要+1
        curDate = c.get(Calendar.DATE)+1;
        curHour = c.get(Calendar.HOUR_OF_DAY );
        if(curHour < 23){
            curHour = curHour +1;
        }
        curSelectYear = curYear;
        curSelectMonth = curMonth;
        curSelectDate = curDate;
        initYear = curYear;
        initMonth = curMonth;
        initDate = curDate;

        initYear(curYear);
        initMonth();
        initDay(curYear, curMonth);
        wheelViewYear.setCurrentItem(curYear - 2000);
        wheelViewMonth.setCurrentItem(curMonth - 1);
        wheelViewDay.setCurrentItem(curDate - 1);
        wheelViewYear.setVisibleItems(5);
        wheelViewMonth.setVisibleItems(5);
        wheelViewDay.setVisibleItems(5);
        selectDateFinish();
        selTimeFinish();
    }

    public void setChangeDate(NovelCreateSentTimeDialog.ChangeDate changeDateListener) {
        this.changeDateListener = changeDateListener;
    }

    /**
     *
     * @param year
     * @param month
     * @return
     */
    private int getDay(int year, int month) {
        int day = 30;
        boolean flag = false;
        switch (year % 4) {
            case 0:
                flag = true;
                break;
            default:
                flag = false;
                break;
        }
        switch (month) {
            case 1:
            case 3:
            case 5:
            case 7:
            case 8:
            case 10:
            case 12:
                day = 31;
                break;
            case 2:
                day = flag ? 29 : 28;
                break;
            default:
                day = 30;
                break;
        }
        return day;
    }
    /**
     * 初始化年
     */
    private void initYear(int curYear) {
        NumericWheelAdapter numericWheelAdapter = new NumericWheelAdapter(getActivity(),2000, 2200);
        numericWheelAdapter.setLabel(" 年");
        //		numericWheelAdapter.setTextSize(15);  设置字体大小
        wheelViewYear.setViewAdapter(numericWheelAdapter);
        wheelViewYear.setCyclic(true);

    }

    /**
     * 初始化月
     */
    private void initMonth() {
        NumericWheelAdapter numericWheelAdapter = new NumericWheelAdapter(getActivity(),1, 12, "%02d");
        numericWheelAdapter.setLabel(" 月");
        //		numericWheelAdapter.setTextSize(15);  设置字体大小
        wheelViewMonth.setViewAdapter(numericWheelAdapter);
        wheelViewMonth.setCyclic(true);
    }

    /**
     * 初始化天
     */
    private void initDay(int arg1, int arg2) {
        NumericWheelAdapter numericWheelAdapter=new NumericWheelAdapter(getActivity(),1, getDay(arg1, arg2), "%02d");
        numericWheelAdapter.setLabel(" 日");
        //		numericWheelAdapter.setTextSize(15);  设置字体大小
        wheelViewDay.setViewAdapter(numericWheelAdapter);
        wheelViewDay.setCyclic(true);
    }

    private void selectDateFinish(){
        int dayCount = getDay(curSelectYear ,curSelectMonth);
        initDay(curSelectYear , curSelectMonth);
        if(dayCount < curSelectDate ){
            curSelectDate = dayCount ;
        }
        wheelViewDay.setCurrentItem(curSelectDate -1 );
        String monthStr = String.format( "%02d",curSelectMonth );
        String dayStr = String.format( "%02d",curSelectDate );
        String dateTime = curSelectYear + "-" + monthStr  + "-"+ dayStr;
        if(changeDateListener != null){
            changeDateListener.changeDateListener(dateTime);
        }
    }
    private void selTimeFinish(){
        if(changeDateListener != null){
            changeDateListener.changeTimeListener(curHour,0);
        }
    }
}
